package util;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import pojo.MerchantDto;

/**
 * Created by CHENCO7 on 7/28/2017.
 */
public class HttpUtil {

    public static String getResponseStr(String uri) throws Exception{
        byte[] buf = null;
        InputStream in = null;
        HttpURLConnection httpURLConnection = null;
        try {
            URL url = new URL(uri);
            URLConnection connection = url.openConnection();
            httpURLConnection = (HttpURLConnection) connection;
            httpURLConnection.setRequestMethod("GET");
            int len = httpURLConnection.getContentLength();
            in = httpURLConnection.getInputStream();
            buf = new byte[len];
            in.read(buf);
        } catch (IOException e) {
            throw new Exception(e);
        } finally {
            try {
                in.close();
                httpURLConnection.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                
            }
        }
        return new String(buf);
    }
    
    public static void main(String[] args) throws Exception {
    	String i = HttpUtil.getResponseStr("http://ZHA-ITA077-w7:8080/admins/c/all");
    	System.out.println(i);
    	List<MerchantDto> t = (List<MerchantDto>) JsonUtil.toJsonObject(i, List.class);
    	System.out.println(t.size());
	}
}
